#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import os
from utils import read_config, html_util
from webs.service import table_service, login_service, config_service, common_service, board_service
from service import users_service

static_path = os.path.join(read_config.base_path, 'webs', 'static')


# 路由页面
def route(url, params):
    params['url'] = url
    if url == config_service.url0:
        response_body = format(trigger_table(params, 1))
    elif url == config_service.url1:  # 总体触发情况
        response_body = format(trigger_table(params, 2))
    elif url == config_service.url2:  # 登录页面
        response_body = login_service.get_login_html()
    elif url == config_service.url3:  # 登录校验
        if login_service.login_check(params):
            response_body = format(trigger_table(params, 1))
        else:
            response_body = login_service.get_login_html()
    elif url == config_service.url4:  # 所有监控代码
        response_body = format(monitor_stock(params, 2))
    elif url == config_service.url5:  # 用户监控股票配置
        response_body = format(user_stock_config(params))
    elif url == config_service.url11:  # 所有监控代码
        response_body = format(all_stock(params))
    elif url == config_service.url12:  # 添加股票到股票池
        response_body = format(add_stock_to_pool(params))
    else:
        response_body = format(trigger_table(params, 1))
    return response_body


respond_msg = {'code': 200, 'msg': 'success'}


# 用户监控的代码
def user_stock_config(params):
    if config_service.username in params.keys():
        params['text'] = board_service.get_user_monitor(params)
    else:
        respond_msg['msg'] = '未登录'
        params['respond_msg'] = respond_msg
        params['text'] = bytes(
            '<li class="btn btn-success outline-btn round"><a href="' + config_service.url2 + '"> 登录 </a></li>',
            encoding='utf-8')
    return params


# 添加股票到股票池
def add_stock_to_pool(params):
    add_msg = users_service.monitor_add(params['stockCode'])
    params['text'] = board_service.get_all_stock()
    params['respond_msg'] = add_msg
    return params


# 所有股票编码
def all_stock(params):
    params['text'] = board_service.get_all_stock()
    return params


# 触发记录
# flag 1:用户监控 2:全部监控
def monitor_stock(params, flag):
    if flag == 2:
        params['text'] = board_service.get_all_monitor()
    return params


# 触发记录
# flag 1:查询当前触发 2:查询全部触发
def trigger_table(params, flag):
    # 获取动态数据\
    text = b''
    try:
        text += table_service.get_trigger_table(flag)
    except:
        print('获取数据错误')
    params['text'] = text
    return params


# 格式化页面
def format(params):
    params = common_service.head(params)  # 页面标题行
    params = common_service.menu(params)  # 页面左侧面板
    params = main_body(params)  # 页面显示主体
    params = common_service.tail(params)  # 页面尾部
    return params['web']


# 显示主题部分
def main_body(params):
    # 获取动态数据\
    params['web'] += b'<div class="main-content">'
    params = common_service.header_section(params)
    params = common_service.wrapper(params)
    params['web'] += b'</div>'
    return params
